﻿using EASYMAILSMTPLib;
using Ndex.Core.Resources;
using System;
using System.IO;

namespace Ndex.Core
{
    public class EasyMailSMTP
    {
        /// <summary>
        /// SendEasyMail
        /// </summary>
        /// <param name="objSMTP"></param>
        /// <param name="name">The friendly name of the recipient. e.g. "John Doe" If you do not want to indicate the friendly name, pass the value as an empty string. e.g. string.Empty</param>
        /// <param name="email">The e-mail address of the recipient e.g. "jdoe@thierdomain.com".</param>
        public static bool Send(string subject, string body, string from, string to, string attachment="")
        {
            var result = true;
            try
            {
                var smtp = new EasyMailSMTPObj();
                smtp.LicenseKey = Web.SettingManager.Get<string>("EasyMailSMTP.License");
                smtp.MailServer = Web.SettingManager.Get<string>("EasyMailSMTP.ServerIP");
                var url = Web.SettingManager.Get<string>("EasyMailSMTP.Url");

                var fromEmail = from.ParseEmail();
                var toEmail = to.ParseEmail();
                // The address, in which system will send a feedback to inform of failed emails
                // So the G-Lock Email Processor will search in this address to determine.
                smtp.From = fromEmail.Name;
                smtp.FromAddr = fromEmail.Email;

                smtp.CharSet = "UTF-8";
                smtp.BodyFormat = 1;//1 HTML - See remarks below.
                smtp.AutoWrap = 0;//If you are sending HTML formatted messages, this property should be set to zero.

                //set the subject and body for the email
                smtp.BodyText = body;
                smtp.Subject = subject;

                smtp.Clear(1 + 4);
                smtp.AddRecipient(toEmail.Name, toEmail.Email, 1);//1: Normal recipient
                smtp.AddCustomHeader("ToEmail", to);
                //add the attach file to the email to send
                if (attachment != string.Empty)
                {
                    try
                    {
                        smtp.AddAttachment(attachment, 0);//0: No options selected (default)
                    }
                    catch (Exception)
                    {
                        //Reserved for future use, must be set to zero.
                        smtp.Disconnect(0);
                        throw new Exception(Strings.EasySendMail_Error_CanNotAttachFile);
                    }
                }

                var errorCode = smtp.SubmitToExpress(url);
                result = errorCode == 0;
                //result: Zero if successful, non zero if the operation failed.
                if (!result)
                {
                    Logger.LogError(Strings.EasySendMail_Error_WithCode 
                        + result.ToString() + " (Error code: " + errorCode + ")");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                result = false;
            }
            return result;
        }

        public static void DeleteFile(string attachFileName)
        {
            if (attachFileName != string.Empty)
            {
                try
                {
                    FileInfo file = new FileInfo(attachFileName);
                    if (file.Exists)
                        file.Delete();
                }
                catch { }
            }
        }
    }
}
